<?php
namespace StarCloud\Services;

use StarCloud\Library\Exception;
use StarCloud\Models\Base;

class SortService extends BaseService
{
    const TYPE_NEWS = 'news';
    const TYPE_BANNER = 'banner';
    const CLASS_MAP = [
        self::TYPE_NEWS => 'StarCloud\Models\Huoshi\News',
        self::TYPE_BANNER => 'StarCloud\Models\Huoshi\Banner',
    ];

    public static function putSort($type, array $ids)
    {
        if (empty($class = self::CLASS_MAP[$type])) {
            throw new Exception(10002);
        }
        /* @var $class Base */
        self::simpleTransaction(function () use ($class, $ids) {
            call_user_func_array([$class, 'resetSort'], []);

            foreach ($ids as $index => $id) {
                call_user_func_array([$class, 'updateSort'], [$id, $index + 1]);
            }
        });

        return true;
    }

    public static function getDbSortList($type)
    {
        if (empty($class = self::CLASS_MAP[$type])) {
            throw new Exception(10002);
        }

        return call_user_func_array([$class, 'getSortList'], []);
    }
}
